from deepface import DeepFace
import cv2

#antispoof
def face_antispoof(image_path):
    print("face antispoof...")
    img = cv2.imread(image_path)
    #face extract
    face_objs = DeepFace.extract_faces(
      img_path = image_path,
      anti_spoofing = True,
    )
    #result  
    for obj in face_objs:
        facial_area = obj["facial_area"]
        print("facial:",facial_area)
        print("conf:",obj["confidence"])
        print("anti_score:",obj["antispoof_score"])
        x = int(facial_area["x"])
        y = int(facial_area["y"])
        w = int(facial_area["w"])
        h = int(facial_area["h"])
        cv2.rectangle(img,(x,y),(x+w,y+h),(127,127,255),3)
        cv2.circle(img,tuple(facial_area["left_eye"]),10,(0,0,255),-1)
        cv2.circle(img,tuple(facial_area["right_eye"]),10,(0,0,255),-1)
        #判斷
        is_real = obj["is_real"]
        print("is_real:",is_real)
        if is_real:
            cv2.putText(img,"True face",(x,y-2),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,0,0),3)
        else:
            cv2.putText(img,"False face",(x,y-2),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255),3)
        #cv2.imshow("result",img)    
        cv2.imwrite("./anti.jpg",img)
        #print("face_objs:",face_objs)

if __name__=='__main__':
    #face antispoof
    path = "./dataset/img1.jpg"
    face_antispoof(path)
